<?php
/**
 * Created by PhpStorm.
 * User: adminchyistrator
 * Date: 2018/12/18
 * Time: 16:58
 */

namespace app\adminchy\controller;

use think\facade\Request;
use think\facade\Session;
use app\adminchy\controller\Base;
use think\Db;


class Room extends Base
{
    //添加房间
    public function addRoom(){
        if (Request::isPost()){
            $data=Request::post();
            if (!$data['room_name']){
                return json(['status'=>1,'message'=>'房间名称不能为空!']);
            }
            if ($data['type'] !=1 && $data['type'] !=2){
                return json(['status'=>1,'message'=>'类型选择错误']);
            }
            if ($data['term'] <0||!is_numeric($data['term'])){
                return json(['status'=>1,'message'=>'过期时间有误']);
            }
            if (!$data['pocket']||$data['pocket'] <0||!is_numeric($data['pocket'])){
                return json(['status'=>1,'message'=>'红包份数有误']);
            }
            if (!$data['money']||$data['money']<0.01){
                return json(['status'=>1,'message'=>'单包金额有误']);
            }
            if ($data['robot_switch'] !=1 && $data['robot_switch'] !=2){
                return json(['status'=>1,'message'=>'机器人开关错误']);
            }
            if (!$data['send_frequency']||$data['send_frequency'] <0||!is_numeric($data['send_frequency'])){
                return json(['status'=>1,'message'=>'机器人发包频率错误']);
            }
            $data = [
                'room_name'=>$data['room_name'],
                'odds'=>$data['odds'],
                'gold_range'=>$data['gold_range'],
                'type'=>$data['type'],
                'money'=>$data['money'],
                'game_intro'=>$data['game_intro'],
                'pocket'=>$data['pocket'],
                'term'=>$data['term'],
                'robot_num'=>$data['robot_num'],
                'robot_switch'=>$data['robot_switch'],
                'send_frequency'=>$data['send_frequency'],
                'create_time'=>date('Y-m-d H:i:s',time()),
            ];
            $res = db('room')->insert($data);
            if ($res){
                return json(['type'=>$data['type'],'status'=>0,'message'=>'添加成功']);
            }else{
                return json(['status'=>1,'message'=>'添加失败']);
            }
        }else{
            return view('Room/addRoom');
        }
    }

    //扫雷房间列表type=1
    public function sweepRoomList(){
        $search = Request::param('search');
        $sql = db('room')->where('type',1);

        if ($search){
            $sql->where('room_name',$search);
        }
        $roomList=$sql->paginate(15)->each(function ($item,$key){
            $item['robot_name'] = implode(',',db('user')->where('user_id','in',$item['robot_uid'])->column('user_name'));
            return $item;
        });

        $this->assign('search',$search);
        $this->assign('roomList',$roomList);
        return view('Room/sweepRoomList');
    }
    //接龙房间列表type=2
    public function solitaireRoomList(){
        $search = Request::param('search');
        $sql = db('room')->where('type',2);

        if ($search){
            $sql->where('room_name',$search);
        }
        $roomList=$sql->paginate(15)->each(function ($item,$key){
            return $item;
        });

        $this->assign('search',$search);
        $this->assign('roomList',$roomList);
        return view('Room/solitaireRoomList');
    }
    //扫雷房间信息
    public function sweepRoomInfo(){
        $id = Request::param('room_id');
        $room = db('room')->where('room_id',$id)->find();
        $this->assign('room', $room);
        return view('Room/sweepRoomInfo');
    }
    //修改房间信息
    public function sweepEditRoom(){
        $data = Request::post();
        if ($data['term'] <0){
            return json(['status'=>1,'message'=>'过期时间有误']);
        }
        if ($data['pocket'] <0){
            return json(['status'=>1,'message'=>'红包份数有误']);
        }
        if ($data['money']<0.01){
            return json(['status'=>1,'message'=>'单包金额有误']);
        }
        $result = db('room')->where(['room_id'=>$data['room_id']])->update($data);
        if ($result) {
            return json(['status' => 0, 'message' =>'修改成功!']);
        } else {
            return json(['status' => 1, 'message' =>'修改失败!']);
        }
    }
    //接龙房间信息
    public function solitaireRoomInfo(){
        $id = Request::param('room_id');
        $room = db('room')->where('room_id',$id)->find();
        $this->assign('room', $room);
        return view('Room/solitaireRoomInfo');
    }
    //修改房间信息
    public function solitaireEditRoom(){
        $data = Request::post();
        if ($data['term'] <0){
            return json(['status'=>1,'message'=>'过期时间有误']);
        }
        if ($data['pocket'] <0){
            return json(['status'=>1,'message'=>'红包份数有误']);
        }
        if ($data['money']<0.01){
            return json(['status'=>1,'message'=>'单包金额有误']);
        }
        $result = db('room')->where(['room_id'=>$data['room_id']])->update($data);
        if ($result) {
            return json(['status' => 0, 'message' =>'修改成功!']);
        } else {
            return json(['status' => 1, 'message' =>'修改失败!']);
        }
    }
    //删除房间
    public function delete_room(){
        $room_id = Request::param('room_id');
        $res = db('room')->where(['room_id'=>$room_id])->find();
        if (!$res){
            return json(['status'=>1,'message'=>'该房间不存在']);
        }
        $delRoom = db('room')->where(['room_id'=>$room_id])->delete();
        if ($delRoom){
            return json(['status'=>0,'message'=>'删除房间成功']);
        }else{
            return json(['status'=>1,'message'=>'删除房间失败']);
        }
    }

    //关闭房间
    public function room_switch(){
        $data = Request::param();
        if(!$data['room_id'] || !$data['switch']) return json_error('参数错误');
        $rN = Db::name('room')->where('room_id',$data['room_id'])->update(['room_switch' => $data['switch']]);
        $mes = $data['switch'] == 1 ? '开启' : '关闭';
        if($rN) return json_success('房间'.$mes.'成功'); else return json_error('房间'.$mes.'失败');
    }

    //添加奖励设置
    public function addPrize(){
        if (Request::isPost()){
            $data = Request::post();
            if ($data['prize_money']<0.01){
                return json(['status'=>1,'message'=>'奖励金额格式错误']);
            }
            $data=[
                'room_id'=>$data['room_id'],
                'snatch_money'=>trim($data['snatch_money']),
                'type'=>$data['type'],
                'prize_money'=>trim($data['prize_money']),
                'create_time'=>date('Y-m-d H:i:s',time())
            ];
            $types = db('room')->where(['room_id'=>$data['room_id']])->value('type');
            $res = db('prize_rule')->insert($data);
            if ($res){
                return json(['type'=>$types,'status'=>0,'message'=>'添加奖励设置成功']);
            }else{
                return json(['status'=>1,'message'=>'添加奖励设置失败']);
            }
        }else{
            $id = Request::param('room_id');
            $this->assign('id',$id);
            return view('Room/addPrize');
        }
    }

    //添加机器人
    public function addRobot()
    {
        if (Request::isPost())
        {
            $data = Request::post();
            if (!$data['rName'] || !$data['rGold']) return json_error('请填写全部参数');
            $arr['create_time']     = date('Y-m-d H:i:s');
            $arr['is_robot']        = 1;
            $arr['user_name']       = $data['rName'];
            $arr['user_gold']       = $data['rGold'];
            $uN = db('user')->where(['user_name'=>$data['rName']])->find();
            if ($uN) return json_error('改昵称已存在');
            Db::startTrans();
            try {
                $uid = Db::name('user')->insertGetId($arr);
                if($uid) $rN = Db::name('room')->where('room_id',$data['room_id'])->update(['robot_uid' => Db::name('room')->where('room_id',$data['room_id'])->value('robot_uid').','.$uid]); else json_error('添加失败');
                if ($rN) {Db::commit(); return json_success('添加成功');} else {Db::rollback(); return json_error('添加失败');}
            } catch (Exception $e) {
                Db::rollback();
                return json_error('添加失败');
            }
        }
        else
        {
            $id = Request::param('room_id');
            $this->assign('id',$id);
            return view('Room/addRobot');
        }
    }

    // 修改机器人状态
    public function edit_robot()
    {
        $data = Request::param();
        if(!$data['room_id'] || !$data['switch']) return json_error('参数错误');
        $rN = Db::name('room')->where('room_id',$data['room_id'])->update(['robot_switch' => $data['switch']]);
        $mes = $data['switch'] == 1 ? '开启' : '关闭';
        if($rN) return json_success('机器人'.$mes.'成功'); else return json_error('机器人'.$mes.'失败');
    }
}